<?php
	require_once('../class/SanPhamDAO.php');
	require_once('../class/LoaiSanPhamDAO.php');
	require_once('../class/ChiTietDonDatHangDAO.php');
	$maGH = $_REQUEST['maGianHang'];
	$tenSP = $_REQUEST['txtTenSanPham'];
	$maLoaiSP = $_REQUEST['selectDanhMucSP'];
	$giaSPTu = $_REQUEST['txtGiaTu'];
	$giaSPDen = $_REQUEST['txtGiaSPDen'];
	$tuNgay = $_REQUEST['txtTuNgay'];
	$denNgay = $_REQUEST['txtDenNgay'];
	$giamTu = $_REQUEST['txtGiamGiaTu'];
	$giamDen = $_REQUEST['txtGiamGiaDen'];
	
	$DSSanPham = SanPhamDAO::TimKiemSanPham_GianHang($maGH, $tenSP, $maLoaiSP, $giaSPTu, $giaSPDen, $tuNgay, $denNgay, $giamTu, $giamDen);
	$tbDSSanPham = "";
	if(count($DSSanPham) >0)
	{
		$tbDSSanPham = '<table width="100%" border="0" cellspacing="0" cellpadding="5">
						  <tr>
							<td>
								<input name="check_All" type="checkbox" id="check_All" onclick="checkAll()"/>
								<input id="txtListCheck" type="hidden" value="" />
							</td>
							<th width="5%">STT</th>
							<th width="5%">Mã</th>
							<th width="15%">Tên sản phẩm</th>
							<th width="14%">Đơn giá gốc </th>
							<th width="13%">Đơn giá bán</th>
							<th width="14%">Số lượng tồn</th>
							<th width="15%">Loại sản phẩm</th>
							<th width="10%">Cập nhật</th>
							<th width="6%">Xóa</th>
						  </tr>';
		
		$check_area = '<input name="chkItem" type="checkbox" onclick="updateList1();" />';
		$stt = 0;
		foreach($DSSanPham as $sanPham)
		{
			$stt ++;
			$loaiSP = LoaiSanPhamDAO::LayLoaiSanPhamTheoMaLoai($sanPham->MaLoaiSP);
			$soLuongTon = $sanPham->SoLuong - SanPhamDAO::TinhSoLuongSanPhamDaMua($sanPham->Ma);
			
			$btnUpd = '<input name="btnCapNhatSP" type="button" value="Cập nhật" onclick="funcCapNhatSP('.$sanPham->Ma .','.$maGH.','.$stt.')"/>';
			
			$btnDel = '<input name="btnXoaSP" type="button" value="Xóa" onclick="funcXoaSP('.$sanPham->Ma.')"/>';
			$check_area = '<input name="chkItem" type="checkbox" onclick="updateList1();" value="'.$sanPham->Ma.'"/>';
			
			$tbDSSanPham .= ' <tr id="SP'.$sanPham->Ma.'">
								<td>
									'.$check_area.'
								</td>
								<td>'.$stt.'</td>
								<td>'.$sanPham->Ma.'</td>
								<td>
									<a href="chi_tiet_san_pham.php?maSanPham='.$sanPham->Ma.'&maGianHang='.$maGH.'" >
										'.$sanPham->TenSanPham.'
									</a>
								</td>
								<td>'.$sanPham->DonGiaGoc.'</td>
								<td>'.$sanPham->DonGiaBan.'</td>
								<td>'.$soLuongTon.'</td>
								<td>'.$loaiSP->TenLoaiSP.'</td>
								<td>'.$btnUpd.'</td>
								<td>'.$btnDel.'</td>
							</tr>
								';
			
		}
		$tbDSSanPham .= '</table>';
		$tbDSSanPham .= '<input name="txtXoaNhieuSP" type="button" value="Xóa nhiều sản phẩm" class="ui-state-default ui-state-hover" onclick="funcXoaNhieuSP();"/> ';
	}
	else 
		$tbDSSanPham .= "<br><br><p class='ui-state-highlight ui-corner-all' 
							style='text-align:center; padding:5px'>
							Không tìm thấy sản phẩm nào!<br></p>";
		
	echo $tbDSSanPham;
?>